using ImageCSharp.Core.Interfaces;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using System.ComponentModel.Composition;
using SixLabors.ImageSharp.Drawing.Processing;

namespace ImageCSharp.Plugins.Filters
{
    /// <summary>
    /// 运动模糊滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class MotionBlurFilter : IFilterPlugin
    {
        public string Name => "运动模糊";
        public string Description => "模拟运动模糊效果";
        public string Category => "模糊";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Angle", 0.0f },
            { "Distance", 10.0f }
        };

        public Image ProcessImage(Image image)
        {
            var angle = Parameters.ContainsKey("Angle") ? (float)Parameters["Angle"] : 0.0f;
            var distance = Parameters.ContainsKey("Distance") ? (float)Parameters["Distance"] : 10.0f;
            
            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();
            //result.Mutate(x => x.MotionBlur(angle, distance));
            result.Mutate(x => ApplyMotionBlur(x, angle, distance));
            return result;
        }

        private void ApplyMotionBlur(IImageProcessingContext context, float angle, float distance)
        {
            int length = Math.Max(1, (int)distance);
            float radius = length / 2f;          // 模糊半径

            // 根据角度选择方向
            switch (angle)
            {
                case >= 45 and < 135:      // 垂直
                                           // ImageSharp 没有“方向”模糊，只能先做垂直方向的 BoxBlur
                    context.BoxBlur((int)radius);
                    break;

                case >= 135 and < 225:     // 水平反向
                    context.BoxBlur((int)radius);
                    break;

                case >= 225 and < 315:     // 垂直反向
                    context.BoxBlur((int)radius);
                    break;

                default:                   // 水平（0-45、315-360）
                    context.BoxBlur((int)radius);
                    break;
            }
        }
    }

    /// <summary>
    /// 径向模糊滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class RadialBlurFilter : IFilterPlugin
    {
        public string Name => "径向模糊";
        public string Description => "模拟径向模糊效果";
        public string Category => "模糊";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Amount", 10.0f },
            { "CenterX", 0.5f },
            { "CenterY", 0.5f }
        };

        public Image ProcessImage(Image image)
        {
            var amount = Parameters.ContainsKey("Amount") ? (float)Parameters["Amount"] : 10.0f;
            var centerX = Parameters.ContainsKey("CenterX") ? (float)Parameters["CenterX"] : 0.5f;
            var centerY = Parameters.ContainsKey("CenterY") ? (float)Parameters["CenterY"] : 0.5f;
            
            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();

            // 实现径向模糊效果
            // 这里使用简化的径向模糊实现
            var center = new PointF(centerX * image.Width, centerY * image.Height);
            result.Mutate(x => 
            {
                // 使用高斯模糊模拟径向模糊效果
                x.GaussianBlur(amount / 10.0f);
            });
            
            return result;
        }
    }
}